package text;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import text.traversal.ITextSetManagerTraversal;
import text.traversal.ITextTraversal;
import text.traversal.WordToolRemover;


public class TextSetManager extends TextSet
{
	protected final List<TextSet>	textSetList	= new LinkedList<TextSet>();
	
	public TextSetManager(String name)
	{
		super(name, null);
	}	
	
	public void addTextSet(TextSet textSet)
	{
		if(textSet.getTextSetManager() != this)
		{
			System.err.println("TextSetManager invalid");
		}
		
		this.textSetList.add(textSet);
		
		for(Text text : textSet.getTextList())
		{
			this.addText(text);
		}
	}
	
	public void removeToolWordsFromAntiDico()
	{
		TextSet antiDico = new TextSet("AntiDico", this);
		try
		{
			antiDico.parseZip("./ressource/antidico.zip");
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		
		ITextTraversal traversal = new ITextTraversal()
		{
			public void traversal(String word, Text text)
			{
				removeWord(word);
			}
		};
		
		antiDico.traversal(traversal);
	}
	
	public void removeToolWords(int minOccurenceCount)
	{
		System.out.print("Removing tool word (occurence count < " + minOccurenceCount + ")...");
		
		WordToolRemover wordToolRemover = new WordToolRemover(minOccurenceCount);
		this.traversal(wordToolRemover);
		
		System.out.println("done");
	}
	
	public void traversal(ITextSetManagerTraversal traversal)
	{
		for (TextSet textSet : this.textSetList)
		{
			traversal.traversal(textSet, this);
		}
	}
	
	@Override
	public boolean removeWord(String word)
	{
		for(TextSet textSet : this.textSetList)
		{
			textSet.removeWord(word);
		}
		
		return super.removeWord(word);
	}
	
	public void traversalAllTextSet(ITextTraversal traversal)
	{
		for(TextSet textSet : this.textSetList)
		{
			textSet.traversal(traversal);
		}
	}
	
	@Override
	public String toString()
	{
		return this.toString(Text.OCCURENCE_INFO);
	}

	public String toString(String info)
	{
		String s = super.toString(info);
		
		s += "All intern text class : " + System.getProperty("line.separator");
		for(TextSet textSet : this.textSetList)
		{
			s += textSet.toString(info);
		}

		return s;
	}
}
